package model.persistence.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.persistence.AbstractDAO;

import model.business.Atend_Social;
import model.business.Atend_SocialImpl;

//HORA, DATA, DESCRICAO, ACESSO_USUARIO, INTERNO_ID


public class PostgresAtend_SocialDAO extends AbstractDAO{

    public Object insert(Object dto) throws SQLException {
        if(!(dto instanceof Atend_Social)) //se algo que nao seja Atend_Social
            throw new SQLException("Tipo de dado inválido para cadastro no banco de dados");

        Atend_Social atend_social = (Atend_Social) dto;

        Connection connection = this.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try{
            statement = connection.prepareStatement("INSERT INTO ATEND_SOCIAL (HORA, DATA, DESCRICAO, ACESSO_USUARIO, INTERNO_ID) VALUES (?,?,?,?,?);"); //prepareStatement - executa comandos SQL, ? separadas por virgula
            statement.setString(1, atend_social.getHora());
            statement.setString(2, atend_social.getData());
            statement.setString(3, atend_social.getDescricao());
            statement.setString(4, atend_social.getAcesso_usuario());
            statement.setString(5, atend_social.getInterno_id());
            statement.execute();

            statement = connection.prepareStatement("SELECT * FROM ATEND_SOCIAL WHERE HORA = ? AND DATA = ? AND DESCRICAO = ? AND ACESSO_USUARIO = ? AND INTERNO_ID = ?");
            statement.setString(1, atend_social.getHora());
            statement.setString(2, atend_social.getData());
            statement.setString(3, atend_social.getDescricao());
            statement.setString(4, atend_social.getAcesso_usuario());
            statement.setString(5, atend_social.getInterno_id());
            
            resultSet = statement.executeQuery();//traz o q esta no cache do banco pra voce
            resultSet.next();

            atend_social = new Atend_SocialImpl(resultSet.getInt("ID"), resultSet.getString("HORA"), resultSet.getString("DATA"), resultSet.getString("DESCRICAO"), resultSet.getString("ACESSO_USUARIO"), resultSet.getString("INTERNO_ID"));

        }catch(SQLException erroBD){
            throw new SQLException("Não foi possível cadastrar o Acesso. \n Motivo:  \n" + erroBD.getMessage());
        }finally{
            if(statement != null)
                statement.close();
        }
        return atend_social;
    }

    public Object update(Object dto) throws SQLException {
         if(!(dto instanceof Atend_Social)) //se algo que nao seja Atend_Social
            throw new SQLException("Tipo de dado inválido para cadastro no banco de dados");

        Atend_Social atend_social = (Atend_Social) dto;

        Connection connection = this.getConnection();
        PreparedStatement statement = null;

        try{
            statement = connection.prepareStatement("UPDATE ATEND_SOCIAL SET HORA = ?, DATA = ?, DESCRICAO = ?, ACESSO_USUARIO = ?, INTERNO_ID = ? WHERE ID = ?;");
            statement.setString(1, atend_social.getHora());
            statement.setString(2, atend_social.getData());
            statement.setString(3, atend_social.getDescricao());
            statement.setString(4, atend_social.getAcesso_usuario());
            statement.setString(5, atend_social.getInterno_id());

            int oid = ((Integer) atend_social.getOID()).intValue();
            statement.setInt(6, oid);
            statement.execute();

        }catch(SQLException erroBD){
            throw new SQLException("Não foi possível alterar o Acesso. \n Motivo: " + erroBD.getMessage());
        }finally{
            if(statement != null)
                statement.close();
        }
        return atend_social;
    }

    public void delete(Object dto) throws SQLException {
        if(!(dto instanceof Atend_Social)) //se algo que nao seja Atend_Social
            throw new SQLException("Tipo de dado inválido para cadastro no banco de dados");

        Atend_Social atend_social = (Atend_Social) dto;

        Connection connection = this.getConnection();
        PreparedStatement statement = null;

        try{
            statement = connection.prepareStatement("DELETE FROM ATEND_SOCIAL WHERE ID = ?;");
            
            int oid = ((Integer) atend_social.getOID()).intValue();
            statement.setInt(1, oid);
            statement.execute();

        }catch(SQLException erroBD){
            throw new SQLException("Não foi possível excluir o Acesso. \n Motivo: " + erroBD.getMessage());
        }finally{
            if(statement != null)
                statement.close();
        }
    }

      public Object find(Object oid) throws SQLException {
        if(!(oid instanceof Integer))
            throw new SQLException("ID não é um número inteiro.");
        int id = ((Integer) oid).intValue();
        Connection connection = this.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Atend_Social atend_social = null; //posso criar tambem Atend_SocialImpl atend_social
        try{
            statement = connection.prepareStatement("SELECT * FROM ATEND_SOCIAL WHERE ID = ?;");
            statement.setInt(1, id);
            resultSet = statement.executeQuery();
            if(!resultSet.next())
                throw new SQLException("Não existe Acesso com id = " + id);
            atend_social = new Atend_SocialImpl(resultSet.getInt("ID"), resultSet.getString("HORA"), resultSet.getString("DATA"), resultSet.getString("DESCRICAO"), resultSet.getString("ACESSO_USUARIO"), resultSet.getString("INTERNO_ID")); //transformando a linha do banco em um objeto

        }catch(SQLException erroBD){
            throw new SQLException("Não foi possível encontrar o Acesso. \n Motivo: " + erroBD.getMessage());
        }finally{
            if(resultSet != null)
                resultSet.close();
            if(statement != null)
                statement.close();
        }
        return atend_social;
    }      
       
}
